/**
 * Title: SubtaskDeptScoreServiceImpl.java
 * @author WuJin
 * @date 2020年6月17日
 * @version 1.0
 */
package com.pm.service.task.impl;

import com.pm.entity.dto.task.TaskQuarterScoreTree;
import com.pm.entity.po.task.TaskCompletionDegree;
import com.pm.entity.vo.ledger.DataAnalysisVo;
import com.pm.entity.vo.task.TeamScoreRankVo;
import com.pm.mapper.awardPunishment.AwardPunishmentMapper;
import com.pm.mapper.ledger.LedgerMapper;
import com.pm.mapper.negative.NegativeChecklistMapper;
import com.pm.mapper.task.SubtaskDeptTaskMapper;
import com.pm.mapper.task.TaskCompletionDegreeMapper;
import com.pm.mapper.task.TaskMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.task.TaskCompletionDegreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Title: SubtaskDeptScoreServiceImpl
 * @author WuJin
 * @date 2020年6月17日
 */
@Service
public class TaskCompletionDegreeServiceImpl extends BaseServiceImpl<TaskCompletionDegree>
        implements TaskCompletionDegreeService {
    @Autowired
    TaskCompletionDegreeMapper subtaskCompletionDegreeMapper;
    @Autowired
    AwardPunishmentMapper awardPunishmentMapper;
    @Autowired
    LedgerMapper ledgerMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    SubtaskDeptTaskMapper subtaskDeptTaskMapper;
    @Autowired
    NegativeChecklistMapper negativeChecklistMapper;

    @Resource
    public void setBasemapper(TaskCompletionDegreeMapper subtaskCompletionDegreeMapper) {
        super.setBaseMapper(subtaskCompletionDegreeMapper);
    }

    @Override
    public List<TaskQuarterScoreTree> getAllScore(TeamScoreRankVo teamScoreRankVo) {
        List<TaskQuarterScoreTree> taskQuarterScoreTreeList = taskMapper.getTaskQuarterScoreTreeList(teamScoreRankVo);
        // 循环赋值
        for (TaskQuarterScoreTree taskQuarterScoreTree : taskQuarterScoreTreeList) {
            if (taskQuarterScoreTree.getChildren().isEmpty()) {
                assembleData(teamScoreRankVo, taskQuarterScoreTree);
            } else {
                for (TaskQuarterScoreTree taskQuarterScoreTree2 : taskQuarterScoreTree.getChildren()) {
                    assembleData(teamScoreRankVo, taskQuarterScoreTree2);
                }
            }
        }
        return taskQuarterScoreTreeList;
    }

    private void assembleData(TeamScoreRankVo teamScoreRankVo, TaskQuarterScoreTree taskQuarterScoreTree) {
        // 循环赋值
        for (int i = 1; i <= 4; i++) {
            teamScoreRankVo.setTime(i);
            teamScoreRankVo.setTaskId(taskQuarterScoreTree.getTaskId());
            Map<String, BigDecimal> map = subtaskDeptTaskMapper.getDeptScoreList(teamScoreRankVo);
            //TaskCompletionChildVo
            if (i == 1) {
                taskQuarterScoreTree.setOneDegree(map.get("degree"));
                taskQuarterScoreTree.setOnePlus(map.get("plus"));
                taskQuarterScoreTree.setOneReduce(map.get("reduce"));
                taskQuarterScoreTree.setOneNegativeScore(map.get("negativeScore"));
                taskQuarterScoreTree.setOneLedgerScore(map.get("ledgeScore"));
                taskQuarterScoreTree.setOneAllScore(
                        taskQuarterScoreTree.getOneDegree().add(taskQuarterScoreTree.getOnePlus()).subtract(taskQuarterScoreTree.getOneReduce())
                                .subtract(taskQuarterScoreTree.getOneNegativeScore()).subtract(taskQuarterScoreTree.getOneLedgerScore())
                );
            }
            if (i == 2) {
                taskQuarterScoreTree.setTwoDegree(map.get("degree"));
                taskQuarterScoreTree.setTwoPlus(map.get("plus"));
                taskQuarterScoreTree.setTwoReduce(map.get("reduce"));
                taskQuarterScoreTree.setTwoNegativeScore(map.get("negativeScore"));
                taskQuarterScoreTree.setTwoLedgerScore(map.get("ledgeScore"));
                taskQuarterScoreTree.setTwoAllScore(
                        taskQuarterScoreTree.getTwoDegree().add(taskQuarterScoreTree.getTwoPlus()).subtract(taskQuarterScoreTree.getTwoReduce())
                                .subtract(taskQuarterScoreTree.getTwoNegativeScore()).subtract(taskQuarterScoreTree.getTwoLedgerScore())
                );
            }
            if (i == 3) {
                taskQuarterScoreTree.setThreeDegree(map.get("degree"));
                taskQuarterScoreTree.setThreePlus(map.get("plus"));
                taskQuarterScoreTree.setThreeReduce(map.get("reduce"));
                taskQuarterScoreTree.setThreeNegativeScore(map.get("negativeScore"));
                taskQuarterScoreTree.setThreeLedgerScore(map.get("ledgeScore"));
                taskQuarterScoreTree.setThreeAllScore(
                        taskQuarterScoreTree.getThreeDegree().add(taskQuarterScoreTree.getThreePlus()).subtract(taskQuarterScoreTree.getThreeReduce())
                                .subtract(taskQuarterScoreTree.getThreeNegativeScore()).subtract(taskQuarterScoreTree.getThreeLedgerScore())
                );
            }
            if (i == 4) {
                taskQuarterScoreTree.setFourDegree(map.get("degree"));
                taskQuarterScoreTree.setFourPlus(map.get("plus"));
                taskQuarterScoreTree.setFourReduce(map.get("reduce"));
                taskQuarterScoreTree.setFourNegativeScore(map.get("negativeScore"));
                taskQuarterScoreTree.setFourLedgerScore(map.get("ledgeScore"));
                taskQuarterScoreTree.setFourAllScore(
                        taskQuarterScoreTree.getFourDegree().add(taskQuarterScoreTree.getFourPlus()).subtract(taskQuarterScoreTree.getFourReduce())
                                .subtract(taskQuarterScoreTree.getFourNegativeScore()).subtract(taskQuarterScoreTree.getFourLedgerScore())
                );
                // 前三季度相加*0.2
                BigDecimal topThreeSumScore = (taskQuarterScoreTree.getOneAllScore().add(taskQuarterScoreTree.getTwoAllScore()).add(taskQuarterScoreTree.getThreeAllScore()))
                        .multiply(new BigDecimal("0.2"));
                // 第四季度*0.4
                BigDecimal fourSumScore = taskQuarterScoreTree.getFourAllScore().multiply(new BigDecimal("0.4"));
                // 全年总得分 = (前三季度相加*0.2 + 第四季度*0.4) * 0.8
                taskQuarterScoreTree.setTaskAllScore(((topThreeSumScore.add(fourSumScore)).multiply(new BigDecimal("0.8"))).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
        }
    }

    @Override
    public List<TaskCompletionDegree> getTeamScoreRank(TeamScoreRankVo teamScoreRankVo) {
        /*String[] month = null;
        switch (teamScoreRankVo.getTime()) {
            case 1:
                month = QuarterEnum.ONE.getMonth().split(",");
                break;
            case 2:
                month = QuarterEnum.TWO.getMonth().split(",");
                break;
            case 3:
                month = QuarterEnum.THREE.getMonth().split(",");
                break;
            case 4:
                month = QuarterEnum.FOUR.getMonth().split(",");
                break;
        }
        teamScoreRankVo.setMonthList(month);*/
        List<TaskCompletionDegree> list = negativeChecklistMapper.getTeamScoreRank(teamScoreRankVo);
        // 排序
        Collections.sort(list, new Comparator<TaskCompletionDegree>() {
            @Override
            public int compare(TaskCompletionDegree u1, TaskCompletionDegree u2) {
                if (u1.getAllScore() == 0) {
                    u1.setRank(1);
                }
                double diff = u1.getAllScore() - u2.getAllScore();
                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                } else {
                    u2.setRank(u1.getRank());
                    return 0; //相等为0
                }
            }
        });
        return list;
    }

    @Override
    public Double getTeamScoreByTaskId(DataAnalysisVo dataAnalysisVo) {
        return negativeChecklistMapper.getTeamScoreByTaskId(dataAnalysisVo);
    }
}
